package com.atguigu.base;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo {
    //可重入锁synchronized和clock都是可重入锁,在同一个线程中外层方法获取锁的时候，再进入该线程的内层方法会自动获取锁在一定程度上可以避免死锁
    public synchronized void a(){
        this.b();
        System.out.println("aaa");
    }
    public synchronized void b(){
        System.out.println("bbb");
    }
    private final Lock lock=new ReentrantLock();
    public void c(){
        lock.lock();
        try {
            this.d();
            System.out.println("ccc");
        }finally {
            lock.unlock();
        }
    }
    public void d(){
        lock.lock();
        try {
            System.out.println("ddd");
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        ReentrantLockDemo reentrantLockDemo = new ReentrantLockDemo();
        //reentrantLockDemo.a();
        reentrantLockDemo.c();
    }
}
